package com.yipingnai.blog.controller;

import com.yipingnai.blog.model.Article;
import com.yipingnai.blog.model.RespBean;
import com.yipingnai.blog.model.RespPageBean;
import com.yipingnai.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: jiangym
 * @Date: 2020/4/11 13:52
 */
@RestController
@RequestMapping("/blog/article")
public class ArticleContoller {
    @Autowired
    ArticleService articleService;
    @GetMapping("/list")
    public RespPageBean getArticle(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer size,String keyword){
        return articleService.getArticle(page,size,keyword);
    }
    @GetMapping("/getDetail")
    public RespBean getDetail(@RequestParam Integer code){
        System.out.println(code+"--code");
        if(code == null){
            return RespBean.error("错误");
        }
        Article article = articleService.getDetail(code);
        if (article != null) {
            return RespBean.ok("文章详情获取成功",article);
        }
        return RespBean.error("获取失败");
    }
    @GetMapping("/category/list")
    public RespPageBean getArticleByCategory(@RequestParam String cname,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer size){
        return articleService.getArticleByCategory(cname,page,size);
    }
    @GetMapping("/hot")
    public List<Article> getHotArticle(){
        List<Article> hotArticle = articleService.getHotArticle();
        System.out.println(hotArticle);
        return hotArticle;
    }
}
